﻿using System.Linq;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Cubicle.Core.Config
{
    [XmlRoot(ElementName="processor")]
    public class ProcessorConfig
    {
        [XmlAttribute(AttributeName="key")]
        public string Key;

        [XmlElement(ElementName = "processor")] 
        public List<ProcessorConfig> Queue = new List<ProcessorConfig>();

        [XmlElement(ElementName = "add")]
        public List<KvpElement> Parameters = new List<KvpElement>();

        public ProcessorConfig() { }
        public ProcessorConfig(string key) { Key = key; }
        public void AddConfig(ProcessorConfig pc) { Queue.Add(pc); }

        public T GetParameter<T>(string key, T fail)
        {
            var candidate = Parameters.FirstOrDefault(p => p.Key == key);
            return candidate == null
                ? fail 
                : (T)System.Convert.ChangeType(candidate.Key, typeof(T));
        }

    }
}